package com.wy.web.old_contact;

import com.wy.pojo.Contact;
import com.wy.service.ContactService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/contact/updateContact")
public class UpdateContactServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       try{
           Map<String, String[]> map = req.getParameterMap();
           Contact contact = new Contact();
           BeanUtils.populate(contact,map);
           ContactService contactService = new ContactService();
           boolean flag=contactService.updateContact(contact);
           if(flag){
               //重定向查询所有联系人信息
               resp.sendRedirect(req.getContextPath()+"/contact/findAll");
           }
//           else{
//               //重定向到错误页面进行委婉提示
//               req.setAttribute("msg","当前功能正在维护");
//               resp.sendRedirect("/contact/update.jsp");
//           }
       }catch (Exception e){
           e.printStackTrace();
       }

    }
}
